// eslint-disable-next-line consistent-return
function initArea(map: { addOverLay: (arg0: any) => void }) {
  // @ts-ignore
  const {T} = window

  // [{"lat":39.95547,"lng":116.275},
  //   {"lat":39.956,"lng":116.32993},
  //   {"lat":39.92546,"lng":116.32341},
  //   {"lat":39.91625,"lng":116.25921}]

    const areaDataArr = [
      {
        id:'001',
        name:'名称A',
        site:'地址A',
        mapArea:[
          [116.275, 39.95547],
          [116.32993, 39.956],
          [116.32341, 39.92546],
          [116.25921, 39.91625]
        ]
      },
      {
        id:'002',
        name:'名称B',
        site:'地址B',
        mapArea:[
          [116.44924, 39.95475],
          [116.54228, 39.95502],
          [116.52786, 39.915],
          [116.45679, 39.89657]
        ]
      }
    ]


    const initOneArea = (item: { id?: string; name: any; site?: string; mapArea: any })=>{
      let polygon: any
      const points: any[] = []

      item.mapArea.forEach((item) => {
        points.push(new T.LngLat(item[0], item[1]))
      })

      // 创建面对象
      polygon = new T.Polygon(points, {
        color: 'blue',
        weight: 1,
        opacity: 0.5,
        fillColor: '#FFFFFF',
        fillOpacity: 0.5
      })
      // 向地图上添加面
      map.addOverLay(polygon)


      function PolygonClick(e) {
        console.log('e', e)
        alert(`您点击了面 ${item.name}`)
      }

      polygon.addEventListener('click', PolygonClick)



      const marker = new T.Marker(new T.LngLat(116.3, 39.935)); // 创建标注

      map.addOverLay(marker);   // 将标注添加到地图中
      const label = new T.Label({
        text: "<div style='width: 90px; border: 1px solid rgba(0,0,0,0.05);'>文字标注!!!<div>",
        position: marker.getLngLat(),
        offset: new T.Point(-65, -60)
      });
      map.addOverLay(label);
    }


    areaDataArr.forEach((item) => {
      initOneArea(item)
    })

  }


export default initArea
